//
//  SetViewController.m
//  WE
//
//  Created by apple on 15/11/24.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "SetViewController.h"
#import "Size.h"
#import "SetTableViewCell.h"
#import "ToolUtil.h"
#import "NumberSetViewController.h"
#import "NewsSetViewController.h"
#import "IntroductionViewController.h"
#import "WelcomeViewController.h"
#import "LeafNotification.h"
#import "MicroMedicalViewController.h"

@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSArray *array;
}
@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"设置";
    self.tabBarController.tabBar.hidden = YES;
    array = @[@"账号设置",@"新消息设置",@"欢迎页",@"功能介绍",@"检查更新"];
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, WindowHeight)];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor colorWithRed:229/255. green:229/255. blue:229/255. alpha:1.0];
    [self.view addSubview:tableView];
    [self setExtraCellLineHidden:tableView];
    
}
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
    
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1){
        return 3;
    
    }else {
        return 1;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 3) {
        static NSString *cellStr = @"LogoutCell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellStr];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellStr];
        }
        
        cell.textLabel.text = @"退出登录";
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
        cell.textLabel.font = FONT(14);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
    
    
    
    static NSString *cellString = @"SetTableViewCell";
    SetTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellString];
    
    if (!cell) {
        cell = [[SetTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellString];
    }
    if (indexPath.section == 0) {
        cell.titleLbl.text = array[0];
    }else if (indexPath.section == 1){
        cell.titleLbl.text = array[indexPath.row + 1];
    }else if(indexPath.section == 2){
        cell.titleLbl.text = array[4];
    }
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 40*Height;
}

-(void)setExtraCellLineHidden: (UITableView *)tableView
{
    UIView *coverView = [UIView new];
    tableView.tableFooterView = coverView;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (!indexPath.section) {
        [self.navigationController pushViewController:[[NumberSetViewController alloc] init] animated:YES];
    }else if(indexPath.section == 1 &&indexPath.row == 0){
        [self.navigationController pushViewController:[[NewsSetViewController alloc] init] animated:YES];
    }else if(indexPath.section == 1 &&indexPath.row == 1){
        [self.navigationController pushViewController:[[WelcomeViewController alloc] init] animated:YES];
    }else if(indexPath.section == 1 &&indexPath.row == 2){
        [self.navigationController pushViewController:[[IntroductionViewController alloc] init] animated:YES];
    }else if(indexPath.section == 2){
        
        [LeafNotification showInController:self withText:@"已是最新版本"];
    }else if (indexPath.section == 3){
         NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
       
        [userDefaults setObject:nil forKey:@"name"];
        
//        self.tabBarController.selectedViewController = UIViewController-YouWant
    
        self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];
        [self.navigationController popToRootViewControllerAnimated:YES];
        //UITabBarController *tab = self.tabBarController;
      
       
        
//        [self.navigationController popToRootViewControllerAnimated:YES];
//        self.tabBarController.selectedIndex = 0;
    }
    
    
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section) {
        return 1;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section) {
        return 10*Height;
    }else{
        return 100*Height;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section) {
        UIView *view = [[UIView alloc] init];
        view.frame = CGRectMake(0, 0, WindowWidth, 10*Height);
        view.backgroundColor = [UIColor colorWithRed:229/255. green:229/255. blue:229/255. alpha:1.0];
        
                        
        
        return view;
        
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WindowWidth, 100*Height)];
        
        return view;
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
